package com.ziyun.pop.rest.module.mall.pay;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.service.common.constant.AppType;
import com.service.order.api.PayService;
import com.service.order.constant.OrderResult;
import com.service.order.model.pay.OrderPayInfoResultDto;
import com.service.order.model.pay.PayParamDto;
import com.ziyun.pop.common.utils.UserUtils;

import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.IPUtils;
import io.renren.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value = "PayRestApi", tags = "商城支付服务")
@RestController
@RequestMapping("popApi/mall/pay/")
public class PayRestApi {
	private static AppType appType = AppType.tuwen_bao;

	@Autowired
	private PayService payService;

	@ApiOperation(value = "查询订单支付信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单编号", required = true, paramType = "query", dataType = "String") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "查询订单支付信息", response = OrderPayInfoResultDto.class))
	@GetMapping(value = "getOrderPayInfo")
	public R getOrderPayInfo(@RequestParam("orderId") String orderId) {
		OrderResult<OrderPayInfoResultDto> paysResult = payService.getOrderPayInfo(appType, UserUtils.getMallUserId(),
				orderId);
		if (R.isError(paysResult.getCode())) {
			return R.error(paysResult.getCode(), paysResult.getMessage());
		}
		return R.apiOk(paysResult.getData());
	}

	@ApiOperation(value = "订单支付")
	@ApiResponses(value = @ApiResponse(code = 200, message = "订单支付"))
	@PostMapping(value = "getOrderPay")
	public R getOrderPay(@RequestBody PayParamDto payParamDto) {
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		String ip = IPUtils.getIpAddr(request);
		OrderResult<?> paysResult = payService.getOrderPay(ip, appType, UserUtils.getMallUserId(), payParamDto);
		if (R.isError(paysResult.getCode())) {
			return R.error(paysResult.getCode(), paysResult.getMessage(), paysResult.getData());
		}
		return R.apiOk(paysResult.getData());
	}

	@ApiOperation(value = "通过支付单号查询支付是否成功")
	@ApiResponses(value = @ApiResponse(code = 200, message = "查询成功"))
	@GetMapping(value = "queryPayment")
	public R queryPayment(@RequestParam("paymentId") String paymentId) {
		OrderResult<?> paysResult = payService.queryPayment(paymentId);
		if (R.isError(paysResult.getCode())) {
			return R.error(paysResult.getCode(), paysResult.getMessage());
		}
		return R.apiOk(paysResult.getData());
	}

}
